<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var getLongestSubstring = function (str) {
            let strArr = [...str];
            let res = 1
            let result = strArr.reduce((total, cur, i, arr) => {
                if (i == 0) {
                    return cur
                } else {
                    if (total.indexOf(cur)<0) {
                        return total + cur
                    } else if (res < total.length) {
                        res = total.length
                        return total.slice(total.indexOf(cur) + 1, total.length) + cur
                    } else {
                        return total.slice(total.indexOf(cur) + 1, total.length) + cur
                    }
                }

            }, "")
            if (res < result.length) {
                res = result.length
            }
            return res
        }
  
        console.log(getLongestSubstring('abcaaa'))
    </script>
</body>

</html>